//
//  HEReagentCheckViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/10.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEReagentCheckViewController.h"
#import "HEReagentCheckCell.h"
#import "HEApplyModel.h"
#import "HECheckItemModel.h"
#import "HEReagentCheckDetailsController.h"

@interface HEReagentCheckViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) NSMutableArray <HECheckItemModel *> * dataSourceArr;

@end

@implementation HEReagentCheckViewController

- (NSMutableArray<HECheckItemModel *> *)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _pageNum = 1;
    _pageSize = 10;
    
    self.title = @"检测报告";
    
    [self setCustomTableView:self.tableView];
    [self showEmpty];
    [self.view addSubview:self.tableView];
    
    [self loadData];
}
 
- (GDTableView *)tableView {
    if (!_tableView) {
        _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight , KScreenWidth, KScreenHeight - ZXNavBarHeight) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollsToTop = YES;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];

        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = CViewBgColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;

        __weak __typeof(&*self)weakSelf = self;
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.isFromHeadRefresh = YES;
            weakSelf.pageNum = 1;
            [weakSelf loadData];
        }];

//        self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
//            weakSelf.isFromHeadRefresh = NO;
//            weakSelf.pageNum += 1;
//            [weakSelf loadData];
//        }];

        [_tableView registerClass:[HEReagentCheckCell class] forCellReuseIdentifier: KHEReagentCheckCell];
    }
    return _tableView;
}


#pragma mark -

- (void)loadData {
    
    HEPageRequest *req = [HEPageRequest new];
    req.pageNum = _pageNum;
    req.pageSize = _pageSize;

    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KcheckInfo parameters:@{} success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);

        if (self.isFromHeadRefresh) {
            [self.dataSourceArr removeAllObjects];
        }

        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];

        [self.dataSourceArr addObjectsFromArray:[HECheckItemModel mj_objectArrayWithKeyValuesArray:response.data]];
        [self.tableView reloadData];

        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];

        if (self.dataSourceArr.count == 0) {
            [self showEmpty];
        }else {
            if (response.data.count == 0) {
                [Dialog toastBottom:@"没有更多数据了~"];
            }
        }
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HEReagentCheckCell *cell = [_tableView dequeueReusableCellWithIdentifier: KHEReagentCheckCell];
    cell.model = self.dataSourceArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier: KHEReagentCheckCell cacheByIndexPath:indexPath configuration:^(HEReagentCheckCell *cell) {
        cell.model = self.dataSourceArr[indexPath.row];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HECheckItemModel *model = self.dataSourceArr[indexPath.row];
 
    HEReagentCheckDetailsController *vc = [HEReagentCheckDetailsController new];
    vc.itemId = model.id;
    [self.navigationController pushViewController:vc animated:YES];
}


@end
